<?php

class Sistema_Form_Gradetamanhos extends Zend_Form
{
    public  $db = '';

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_'.$controller);
        $this->setAttrib('class', 'formCtrl');
        
        $this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $id = $front->getRequest()->getParam('id',0);
        if($id > 0){
            //$obj = new Sistema_Model_DbTable_Gradetamanhos();
            // $data = $obj->fetchRow('GRA_ID = ' . $id)->toArray();
            $query = 'SELECT SQL_CACHE * FROM GRADETAMANHOS WHERE GRA_ID = ' . $id;
            $data = $this->db->fetchRow($query);           
        }
        
        Zend_Loader::loadClass('dominio','library');
        
        $el = new Zend_Form_Element_Hidden('GRA_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('GRA_NOM');
        $el->setLabel('Nome')
        ->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        /*
        $obj = new Sistema_Model_DbTable_Catsegmento();
        $row = $obj->fetchAll()->toArray();
        $arr = array(''=>'Nenhum');
        foreach($row as $r) $arr[$r['CTS_ID']] = $r['CTS_NOM']; 
        
        $el = new Zend_Form_Element_Select('CTS_ID',array('onchange' => ''));
        $el->setLabel('Segmento')->setRequired(true)->addValidator('NotEmpty')->addMultiOptions($arr);
        $this->addElement($el);
        */
        
        $html = '';
        $htmli = '';
        //$obj = new Sistema_Model_DbTable_Catsegmento();
        //$row = $obj->fetchAll($obj->select()->where('CTS_STA = 1')->order('CTS_ORD ASC'))->toArray();
        $query = 'SELECT SQL_CACHE * FROM CATSEGMENTO WHERE CTS_STA = 1 ORDER BY CTS_ORD ASC';
        $row = $this->db->fetchAll($query);
        $arr = array();
        $arr_set = array();
        
        foreach($row as $r){
            
            $count = 0;
            $checked = '';
            
            //$fsc = new Sistema_Model_DbTable_GradetamanhosHasCat();
            //$fsc = $fsc->fetchAll('GRA_ID = '.$id.' and CTS_ID = '.$r['CTS_ID'])->toArray();
            $query = 'SELECT SQL_CACHE CAT_ID FROM GRADETAMANHOS_HAS_CAT WHERE GRA_ID = '.$id.' and CTS_ID = '.$r['CTS_ID'];
            $fsc = $this->db->fetchAll($query);
            foreach($fsc as $f){
                $arr_set[$f['CAT_ID']] = $f['CAT_ID'];
                $count = 1;
            }
            
            //$htmli = '';
            //if($count == 1){
                
                $checked = 'checked=""';
                
                $htmli = '<select id="SEG_CAT['.$r['CTS_ID'].'][]" name="SEG_CAT['.$r['CTS_ID'].'][]" class="chosen-select" multiple="multiple" style="width:500px;">';
            
                //$cat = new Sistema_Model_DbTable_Categoria;
                //$cat = $cat->fetchAll('CTS_ID = '.$r['CTS_ID']);
                //$cat = $cat->fetchAll($cat->select()->where('CTS_ID = '.$r['CTS_ID'])->order('CAT_NOM ASC')); 
                $query = 'SELECT SQL_CACHE CAT_ID,CAT_NOM FROM CATEGORIA WHERE CTS_ID = '.$r['CTS_ID'].' ORDER BY CAT_NOM ASC';
                $cat = $this->db->fetchAll($query); 
                foreach($cat as $c){
                    $selected = array_search($c['CAT_ID'], $arr_set) > '' ? 'selected=""':'';            
                    $htmli .= '<option '.$selected.' value="'.$c['CAT_ID'].'">'.$c['CAT_NOM'].'</option>'; 
                }
                $htmli .= '</select><br><br>';
            //}
            
            $html .= '<div><label>'.$r['CTS_NOM'].'</label>';
            $html .= '<div id="cat'.$r['CTS_ID'].'">'.$htmli.'</div></div>';
        }
        
        $el = new Zend_Form_Element('SEG_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $this->addElement($el);
        
        /*
        $arr = array();
        if($id > 0){
            $obj = new Sistema_Model_DbTable_Categoria();
            $row = $obj->fetchAll('CTS_ID ='.$data['CTS_ID'])->toArray();
            $arr['0'] = 'Todos'; 
            foreach($row as $r) $arr[$r['CAT_ID']] = $r['CAT_NOM']; 
        }
        $el = new Zend_Form_Element_Select('CAT_ID',array('onchange' => ''));
        $el->setLabel('Categoria')->setRegisterInArrayValidator(false)->addMultiOptions($arr);
        $this->addElement($el);
        */
        
        //$obj = new Sistema_Model_DbTable_Modelagem();
        //$row = $obj->fetchAll('MDL_STA = 1')->toArray();
        $query = 'SELECT SQL_CACHE MDL_ID,MDL_NOM FROM MODELAGEM WHERE MDL_STA = 1';
        $row = $this->db->fetchAll($query); 
        $arr = array(''=>'Nenhum');
        foreach($row as $r) $arr[$r['MDL_ID']] = $r['MDL_NOM']; 
        
        $el = new Zend_Form_Element_Select('MDL_ID',array('onchange' => ''));
        $el->setLabel('Modelagem')->setRequired(false)->addValidator('NotEmpty')->addMultiOptions($arr);
        $this->addElement($el);
       
        $arr_set = array();
        if($id > 0){
            //$obj = new Sistema_Model_DbTable_GradetamanhosHasTamanhos();
            //$row = $obj->fetchAll('GRA_ID ='.$data['GRA_ID'])->toArray(); 
            $query = 'SELECT SQL_CACHE TAM_ID FROM GRADETAMANHOS_HAS_TAMANHOS WHERE GRA_ID ='.$data['GRA_ID'];
            $row = $this->db->fetchAll($query);
        
            foreach($row as $r) $arr_set[$r['TAM_ID']] = $r['TAM_ID']; 
        }
        
        $html = '<select id="TAM_ID" name="TAM_ID[]" class="chosen-select" multiple="multiple" style="width:500px;">';
        //$obj = new Sistema_Model_DbTable_Tamanhos();
        //$row = $obj->fetchAll($obj->select()->where('TAM_STA = 1')->order(array('TAM_ORD ASC','TAM_ID ASC'))); 
        $query = 'SELECT SQL_CACHE TAM_ID,TAM_NOM FROM TAMANHOS WHERE TAM_STA = 1 ORDER BY TAM_ORD ASC, TAM_ID ASC';
        $row = $this->db->fetchAll($query);
        $arr = array();
        foreach($row as $r){ 
            $selected = array_search($r['TAM_ID'], $arr_set) > '' ? 'selected=""':'';            
            $html .= '<option '.$selected.' value="'.$r['TAM_ID'].'">'.$r['TAM_NOM'].'</option>'; 
        }
        $html .= '</select>';
        
        $el = new Zend_Form_Element('TAM_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Legenda Tamanhos');
        $this->addElement($el);
        
        
        
        $arr_set = array();
        if($id > 0){
            //$obj = new Sistema_Model_DbTable_GradetamanhosHasLegendaMedidas();
            //$row = $obj->fetchAll('GRA_ID ='.$data['GRA_ID'])->toArray(); 
            $query = 'SELECT SQL_CACHE LGM_ID FROM GRADETAMANHOS_HAS_LEGMED WHERE GRA_ID ='.$data['GRA_ID'];
            $row = $this->db->fetchAll($query);
        
            foreach($row as $r) $arr_set[$r['LGM_ID']] = $r['LGM_ID']; 
        }

        $html = '<select id="LGM_ID" name="LGM_ID[]" class="chosen-select" multiple="multiple" style="width:500px;">';
        //$obj = new Sistema_Model_DbTable_Legendamedidas();
        //$row = $obj->fetchAll($obj->select()->where('LGM_STA = 1')->order(array('LGM_ORD ASC','LGM_ID ASC'))); 
        $query = 'SELECT SQL_CACHE LGM_ID,LGM_NOM FROM LEGENDAMEDIDAS WHERE LGM_STA = 1 ORDER BY LGM_ORD ASC, LGM_ID ASC';
        $row = $this->db->fetchAll($query);
        $arr = array();
        foreach($row as $r){ //$arr[$r['MTL_ID']] = $r['MAT_NOM']; 
            $selected = array_search($r['LGM_ID'], $arr_set) > '' ? 'selected=""':'';            
            $html .= '<option '.$selected.' value="'.$r['LGM_ID'].'">'.$r['LGM_NOM'].'</option>'; 
        }
        $html .= '</select>';
        
        $el = new Zend_Form_Element('LGM_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Legenda Medidas');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_File('GRA_FILE');
        $el->setLabel('Tabela de Tamanhos - Tamanho: 200x200')
	->addValidator('Extension', false, array('jpg', 'png', 'gif'));
        $this->addElement($el);
        
        $img = 'public/upload/gradetamanhos/'.$front->getRequest()->getParam('id').'_tabtam.*';
        foreach (glob($img) as $src) {
            $el = new Zend_Form_Element('img2',array( 'helper'=>'formNote', 'value'=>'<img src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        
        $el = new Zend_Form_Element_Checkbox('GRA_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('submit',array('disableLoadDefaultDecorators' =>true));
        $btn->setLabel('Gravar');
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-button btn-ok');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        //$btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-button btn-cancel">Cancelar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-button btn-cancel');
        $this->addElement($btn);
        
    }


}

